package com.mr.cai.main.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cai.mr.lib.utils.LogInfo;
import com.mr.cai.main.R;
import com.mr.cai.main.activity.base.MrCaiBaseActivity;
import com.mr.cai.main.bean.MrCaiPublicStatusBean;
import com.mr.cai.main.httpApi.MrCaiHttpApi;
import com.mr.cai.main.manager.PreferencesManager;
import com.mr.cai.main.parse.MrCaiPublicStatusBeanParse;
import com.mr.cai.main.utils.CommonCoreUtil;
import com.mr.cai.main.view.LineEditText;
import com.mr.cai.main.view.LoginThirdCountPopup;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Created by hai8108 on 16/2/7.
 */
public class LoginActivity extends MrCaiBaseActivity {
    private View backView;
    private ProgressBar pb_loaing;
    private LineEditText userNameEdt;
    private LineEditText pwdEdt;
    private View forgetPwdView;

    private TextView loginTv;
    private TextView registerTv;

    private LoginThirdCountPopup mLoginThirdCountPopup;
    private View loginWithThirdCountView;

    public static void launchActivity (Activity activity) {
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_layout);
        findView();
    }

    private void findView () {
        pb_loaing = (ProgressBar) findViewById(R.id.pb_loaing);
        mLoginThirdCountPopup = new LoginThirdCountPopup(this);
        backView = findViewById(R.id.login_back);
        forgetPwdView = findViewById(R.id.login_forget_pwd);
        loginWithThirdCountView = findViewById(R.id.login_with_third_count);

        loginTv = (TextView) findViewById(R.id.login_submit);
        registerTv = (TextView) findViewById(R.id.login_register);

        userNameEdt = (LineEditText) findViewById(R.id.user_name_edt);
        pwdEdt = (LineEditText) findViewById(R.id.user_pwd);
        pwdEdt.setBottomLineColor(R.color.color_main_sub_white_gray);
        clickActionProcess();
    }
    private void clickActionProcess () {
        CommonCoreUtil.setClickAction(backView, new Action1() {
            @Override
            public void call (Object o) {
                finish();
            }
        });
        CommonCoreUtil.setClickAction(forgetPwdView, new Action1() {
            @Override
            public void call (Object o) {
                ForgetPwdActivity.launchActivity(LoginActivity.this);
            }
        });
        CommonCoreUtil.setClickAction(loginTv, new Action1() {
            @Override
            public void call (Object o) {
                MainActivity.launchActivity(LoginActivity.this);
                finish();
//                if(judgeInput()){
//                    requestLogin(userNameEdt.getEditableText().toString(), pwdEdt.getEditableText().toString());
//                }
            }
        });
        CommonCoreUtil.setClickAction(registerTv, new Action1() {
            @Override
            public void call (Object o) {
                RegisterActivity.launchActivity(LoginActivity.this);
            }
        });
        CommonCoreUtil.setClickAction(loginWithThirdCountView, new Action1() {
            @Override
            public void call (Object o) {
                mLoginThirdCountPopup.showAtLocation(loginWithThirdCountView, Gravity.RIGHT |
                        Gravity.BOTTOM, 0, 0);
            }
        });
    }

    private boolean judgeInput () {
        String newEmail = userNameEdt.getEditableText().toString();
        String newPassword = pwdEdt.getEditableText().toString();
        if (!CommonCoreUtil.judgeRegxEmailAndMobile(newEmail)) {
            return false;
        }
        boolean passValid = !TextUtils.isEmpty(newPassword);
        if (!passValid) {
            CommonCoreUtil.showToast(R.string.pwd_error);
            return false;
        }
        return true;
    }
    private Subscription registerSubscription;
    private void requestLogin (final String mobileNum, final String pwd) {
        Observable<MrCaiPublicStatusBean> authCodeObservable = MrCaiHttpApi.requestLogin
                (mobileNum, pwd, new MrCaiPublicStatusBeanParse());
        registerSubscription = authCodeObservable.observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call () {
                        pb_loaing.setVisibility(View.VISIBLE);
                    }
                })
                .subscribe(new Subscriber<MrCaiPublicStatusBean>() {
                    @Override
                    public void onCompleted () {
                        pb_loaing.setVisibility(View.GONE);
                    }
                    @Override
                    public void onError (Throwable e) {
                        pb_loaing.setVisibility(View.GONE);
                        CommonCoreUtil.showToast(R.string.data_request_fail);
                    }
                    @Override
                    public void onNext (MrCaiPublicStatusBean mMrCaiPublicStatusBean) {
                        if (mMrCaiPublicStatusBean != null && mMrCaiPublicStatusBean.isResult()
                                && mMrCaiPublicStatusBean.getCode() == 1) {
                            CommonCoreUtil.showToast(R.string.login_sucess_txt);
                            PreferencesManager.getInstance().setUserMobile(mobileNum);
                            PreferencesManager.getInstance().setUserCustomerId
                                    (mMrCaiPublicStatusBean.getData().getCustomerId());
                            LogInfo.log("haitian", "customerId="+mMrCaiPublicStatusBean.getData()
                                    .getCustomerId());
                            MainActivity.launchActivity(LoginActivity.this);
                            finish();
                        } else {
                            if (mMrCaiPublicStatusBean != null) {
                                CommonCoreUtil.showToast(mMrCaiPublicStatusBean.getMsg());
                            } else {
                                CommonCoreUtil.showToast(R.string.login_failed_txt);
                            }
                        }
                    }
                });
    }
    @Override
    protected void onDestroy () {
        super.onDestroy();
        CommonCoreUtil.hideSoftInput(userNameEdt);
        CommonCoreUtil.hideSoftInput(pwdEdt);
    }
}
